package com.yz.lzq.liwushuo.ui.modules.home.nomal;

import com.yz.lzq.liwushuo.http.HttpUtils;
import com.yz.lzq.liwushuo.http.OnGetNetDataListener;
import com.yz.lzq.liwushuo.jsonbean.home.boutique.HomeBoutiqueRecycleBean;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/12/13.
 * 底部导航栏首页下的精品类的fragment的model层的接口
 */

public class HomeNomalModel implements HomeNomalContract.HomeNomalModelInterface {
    /**
     * @param ad         Home里面的精品需要的参数
     * @param id         每个fragment的标志
     * @param gender     性别
     * @param generation 身份
     * @param limit      一次请求的item数量
     * @param offset     当前的下标
     * @param listener   回调接口，将请求的数据返回
     * @param loadMore   是否加载更多
     *                   1.当ad=0时，表示是一般的fragment正在加载数据，否则为精品正在加载
     *                   2.当loadMore为真时，表示为上拉加载
     */
    @Override
    public void LoadHomeNomalRecycleViewBean(int ad, int id, int gender, int generation, int limit, int offset, final OnGetNetDataListener<HomeBoutiqueRecycleBean> listener, boolean loadMore) {
        Map<String, String> map = new HashMap<>();

        if (loadMore) {
            map.put("generation", generation + "");
            map.put("gender", gender + "");
            if (ad != 0) {
                map.put("ad", ad + "");
            }
            map.put("limit", limit + "");
            map.put("offset", offset + "");

        } else {
            if (ad != 0) {
                map.put("ad", ad + "");
            }
            map.put("gender", gender + "");
            map.put("limit", limit + "");
            map.put("offset", offset + "");
            map.put("generation", generation + "");
        }
        Observable<HomeBoutiqueRecycleBean> observable = HttpUtils.getInstance().getIApi().getHomeBoutiqueRecycleBean("channels/" + id + "/items_v2", map);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HomeBoutiqueRecycleBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onGetDataFalied(e);
                    }

                    @Override
                    public void onNext(HomeBoutiqueRecycleBean homeBoutiqueRecycleBean) {
                        listener.onGetDataSucess(homeBoutiqueRecycleBean);
                    }
                });
    }
}

